package com.neusoft.service.impl;

import com.neusoft.common.FTPUtil;
import com.neusoft.service.IFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Service
public class FileServiceImpl implements IFileService {


    @Value("${ftp.remotePath}")
    private String ftpUrl;

    @Override
    public String uploadFile( MultipartFile file) throws Exception {

        // 调用文件上传工具类返回内容
        return FTPUtil.upload(file);
    }

    @Override
    public boolean dwonload(String file, String savePath) throws Exception {
        return FTPUtil.download(file, savePath);
    }

    private String getUuidFileName(String originalFileName){
        String uuid = getUuid();
        if(StringUtils.isBlank(originalFileName)){
            return uuid;
        }

        int i = originalFileName.lastIndexOf('.');
        return -1 == i ? uuid : uuid + originalFileName.substring(i);
    }

    private String getUuid(){
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }
}
